package com.sakura.taos.tool.manipulator.controllers;


import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import com.baomidou.dynamic.datasource.creator.HikariDataSourceCreator;
import com.baomidou.dynamic.datasource.ds.GroupDataSource;
import com.sakura.taos.tool.manipulator.model.LayuiTree;
import com.sakura.taos.tool.manipulator.model.SysDbLink;
import com.sakura.taos.tool.manipulator.model.Variables;
import com.sakura.taos.tool.manipulator.model.layui.LayReturn;
import com.sakura.taos.tool.manipulator.model.layui.LayReturnToData;
import com.sakura.taos.tool.manipulator.model.layui.LayuPage;
import com.sakura.taos.tool.manipulator.service.TaosBaseService;
import com.sakura.taos.tool.manipulator.service.TaosSysService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.*;

import static com.sakura.taos.tool.manipulator.conf.ConstantConf.DATA_BASE_GROUP_MAIN_NAME;
import static com.sakura.taos.tool.manipulator.conf.ConstantConf.DATA_BASE_GROUP_SEPARATOR;

/**
 * 系统信息类
 *
 * @Author: 贺坤
 * @Date: 2021/1/28 14:25
 */
@RestController
@RequestMapping("/sys")
@Slf4j
public class TaosSysController {
    /**
     * 此处不能使用@Autowired ,@Autowired会导致自动注入失败
     * @Resource 与@Autowired  默认的注入策略不同, 尚不清楚为什么在测试中可以使用@Autowired
     *  @Autowired @Qualifier( "DynamicRoutingDataSource" ) 不生效
     *  属性name必须为 dataSource 的时候@Resource 与 @Autowired都 自动注入会成功
     *  @Autowired @Qualifier( "dataSource" ) 生效
     *
     *
     *  推断 @Autowired 优先 byType 找不到 byName
     */
    @Autowired
    @Qualifier( "dataSource" )
    private  DynamicRoutingDataSource dynamicRoutingDataSource;
    @Autowired
    private HikariDataSourceCreator hikariDataSourceCreator;

    @Autowired
    private TaosSysService taosSysService;
    @Autowired
    private TaosBaseService taosBaseService;
    @GetMapping
    public Map<String, DataSource> getCurrentDataSources() {
        Map<String, DataSource> dataSources = dynamicRoutingDataSource.getCurrentDataSources();
//        System.out.println(dataSources);
        return null;
    }

    /**
     * 获取指定数据库系统当前参数
     * @return
     */
    @GetMapping("sysData")
    public LayReturnToData getSysData(){
        List<Variables> sysData = taosSysService.getSysData();
        return new LayReturnToData(0,"ok",sysData);
    }

    /**
     * 得到数据库连接树形结构
     *
     * @return
     */
    @GetMapping("treeData")
    public LayReturnToData<LayuiTree> getTreeData() {
        //根据分组获取数据源
        // 根据对数据源包的设定, 以 _ 为分隔符
        Map<String, GroupDataSource> groupDataSources = dynamicRoutingDataSource.getCurrentGroupDataSources();
        Set<String> keys = groupDataSources.keySet();
        List<LayuiTree> treeList = new ArrayList<>(keys.size());
        // 遍历所有分组的数据源
        for (String key : keys) {
            // 根节点
            LayuiTree tree = taosBaseService.getTreeData(key,key+DATA_BASE_GROUP_SEPARATOR+DATA_BASE_GROUP_MAIN_NAME);
            treeList.add(tree);
        }
        return new LayReturnToData(0,"ok",treeList);
    }

}
